<?php

namespace App\Http\Controllers\Api;

use App\Http\Services\AccountService;
use Exception;
use Illuminate\Http\Request;

/**
 * @property AccountService $accountService
 */
class AccountController extends BaseController
{

    public function __construct(AccountService $accountService)
    {
        $this->accountService = $accountService;
    }

    public function wxLogin(Request $request)
    {
        try {
            return $this->success($this->accountService->wxLogin($request->post('code')));
        } catch (Exception $e) {
            return $this->fail($e->getMessage());
        }
    }

    public function setUserInfo(Request $request)
    {
        $result = $this->accountService->setUserInfo($request->post());
        if (!$result) {
            return $this->fail('设置失败');
        }
        return $this->success();
    }

    public function getUserInfo(Request $request)
    {
        return $this->success($this->accountService->getUserInfo());
    }

    public function refreshToken(Request $request)
    {
        try {
            $result = $this->accountService->refreshToken($request->post('openid'));
            return $this->success($result);
        } catch (Exception $e) {
            return $this->fail($e->getMessage());
        }
    }

    public function clearHistory(Request $request)
    {
        $result = $this->accountService->clearHistory($request->userInfo->id);
        if (!$result) {
            return $this->fail('清除失败');
        }
        return $this->success();
    }

    public function getHistoryList(Request $request)
    {
        return $this->success($this->accountService->getHistoryList($request->post('openid')));
    }

    public function saveHistory(Request $request)
    {
        $result = $this->accountService->saveHistory($request->post());
        if (!$result) {
            return $this->fail('保存失败');
        }
        return $this->success();
    }

    public function addComment(Request $request)
    {
        $result = $this->accountService->addComment($request->post());
        if (!$result) {
            return $this->fail('保存失败');
        }
        return $this->success();
    }

    public function getCommentList(Request $request)
    {
        return $this->success($this->accountService->getCommentList($request->get('question_id')));
    }

    public function toggleLike(Request $request)
    {
        $ret = $this->accountService->toggleLike($request->get('question_id'));
        if (!$ret) {
            return $this->fail('点赞失败');
        }
        return $this->success();
    }

    public function getLikeInfo(Request $request)
    {
        return $this->success($this->accountService->getLikeInfo($request->get('question_id')));
    }

    /**
     * 添加收藏
     */
    public function addFavorite(Request $request)
    {
        $result = $this->accountService->addFavorite($request->get('question_id'));
        if (!$result) {
            return $this->fail('收藏失败，可能已经收藏过了');
        }
        return $this->success();
    }

    /**
     * 取消收藏
     */
    public function removeFavorite(Request $request)
    {
        $result = $this->accountService->removeFavorite($request->get('question_id'));
        if (!$result) {
            return $this->fail('取消收藏失败');
        }
        return $this->success();
    }

    /**
     * 获取收藏列表
     */
    public function getFavoriteList(Request $request)
    {
        return $this->success($this->accountService->getFavoriteList());
    }

    /**
     * 检查是否已收藏
     */
    public function checkFavorite(Request $request)
    {
        return $this->success($this->accountService->checkFavorite($request->get('question_id')));
    }
}
